
package com.eascs.crm.nsEditDetail.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.service.CustomerShopInfoService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

@Controller
@RequestMapping("/customer/shop")
public class CustomerShopInfoViewController extends BaseMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(CustomerShopInfoViewController.class);

	@Autowired
	CustomerShopInfoService shopInfoService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;

	@RequestMapping(value = "/list")
	public String customerShopInfoList(String loanapplyNo, @RequestParam(defaultValue="0") Integer showPage, String type, String approvalStatus) {
		logger.info("查询门店信息，贷款单号：【{}】", loanapplyNo);
		String pageUrl = "/bank/account/mgr?";
		String[] titles = {"门店名称", "门店类型", "成立时间","与EA合作时间", "行业类别" };
		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("shoptype", "CRM"), null, null, new SelectDataOption("vocation", "CRM")};
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
		PageResult<Object[]> pageResult = shopInfoService.page(loanapplyNo);

		Button detail = ButtonHelper.createAjaxButton("详情", "/customer/shop/rest/detailPage?type="+type+"&approvalStatus="+approvalStatus+"&customerShopId=");
		Button update = null;
		Button del = null;
		if (!"detail".equals(type)) {
			if(StatusContexts.O2O_ZX_END.equals(o2oLoanapplyEntity.getWfType())){
				update = ButtonHelper.createAjaxButton("修改", "/customer/shop/rest/updatePage?customerShopId=", "UpdateInternalInfo");
				//update.setPermissionCode("dczllrUpdateButton");
				del = ButtonHelper.createAjaxConfirmButton("删除",  "/customer/shop/rest/del?customerShopId=", "UpdateInternalInfo");
				del.setConfirmMessage("确定删除?");
				//del.setPermissionCode("dczllrUpdateButton");
			}
		}
		//Button look = ButtonHelper.createOpenButton("查看", "/dcloan/baseInfo?billCode={1}");
		//look.setPermissionCode("dczllrUpdateButton");

		Button[] detailButton = new Button[] {detail, update, del};

		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl + "&showPage=");
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		ViewContext.put("paginationInfo", paginationInfo);

		return "nsEditDetail/customer_shop_info_list";

	}
	@RequestMapping(value = "/listnew")
	public String customerShopInfoListNew(String loanapplyNo, @RequestParam(defaultValue="0") Integer showPage, String type, String approvalStatus) {
		logger.info("查询门店信息，贷款单号：【{}】", loanapplyNo);
		String[] titles = {"产权性质",  "门店成立日期","租赁合同起止日期","门店地址" };
		SelectDataOption[] selectDataOptions = { new SelectDataOption("PROPERTYTYPE"), null, null};
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
//		PageResult<Object[]> pageResult = shopInfoService.page(loanapplyNo);
//		PageResult<Object[]> pageResult = shopInfoService.pagenew(loanapplyNo);
		PageResult<Object[]> pageResult = shopInfoService.pagenewSensitive(loanapplyNo,type,approvalStatus);// 脱敏
		
		Button detail = ButtonHelper.createAjaxButton("详情", "/customer/shop/rest/detailPagenew?type="+type+"&approvalStatus="+approvalStatus+"&customerShopId=");
		Button update = null;
		Button del = null;
		Button addressButton = null;
		
		if(StringUtil.isNotBlank(approvalStatus)){
			if (!"UNDO".equals(approvalStatus)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/customer/shop/rest/view_address?customerShopId=", "UpdateInternalInfo");
			}
		}else {
			if (!"detail".equals(type)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/customer/shop/rest/view_address?customerShopId=", "UpdateInternalInfo");
				if(StatusContexts.O2O_ZX_END.equals(o2oLoanapplyEntity.getWfType())){
					update = ButtonHelper.createAjaxButton("修改", "/customer/shop/rest/updatePagenew?customerShopId=", "UpdateInternalInfo");
					//update.setPermissionCode("dczllrUpdateButton");
					del = ButtonHelper.createAjaxConfirmButton("删除",  "/customer/shop/rest/del?customerShopId=", "UpdateInternalInfo");
					del.setConfirmMessage("确定删除?");
					//del.setPermissionCode("dczllrUpdateButton");
				}
			}
		}
		
		//Button look = ButtonHelper.createOpenButton("查看", "/dcloan/baseInfo?billCode={1}");
		//look.setPermissionCode("dczllrUpdateButton");

		Button[] detailButton = new Button[] {detail, update, del};
		
		Button[] titleButtons = new Button[] { null, null, null, addressButton };

		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		ViewContext.put("paginationInfo", paginationInfo);

		return "nsEditDetail/customer_shop_info_list";

	}
}
